package com.aaa.doctor.service.impl;

import com.aaa.doctor.dao.*;
import com.aaa.doctor.feign.DeptFeign;
import com.aaa.doctor.feign.UserFeign;
import com.aaa.doctor.service.PatientService;
import com.aaa.doctor.utils.IdWorker;
import com.aaa.doctor.vo.PatientVo;
import com.aaa.doctor.vo.PatientVoo;
import com.aaa.doctor.vo.Pvo;
import com.aaa.entity.*;
import com.aaa.utils.JWTUtils;
import com.aaa.utils.WebUtil;
import com.aaa.vo.Result;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.*;

/**
 * @version 1.8.0_181
 * @Author ZXJ
 * @Date 2023/3/24 15:19
 * @注释
 */
@Service
public class PatientServiceImpl implements PatientService {
    @Resource
    private PatientDao patientDao;
    @Override
    public Result<IPage<Patient>> searchPatient(Integer currentPage, Integer pageSize, PatientVo patientVo) {
        IPage<Patient> page=new Page<>(currentPage,pageSize);
        QueryWrapper<Patient> wrapper=new QueryWrapper<>();
        if (StringUtils.hasText(patientVo.getName())){
            wrapper.like("name",patientVo.getName());
        }
        if (StringUtils.hasText(patientVo.getPhone())){
            wrapper.eq("phone",patientVo.getPhone());
        }
        if (StringUtils.hasText(patientVo.getIdCard())){
            wrapper.eq("id_Card",patientVo.getIdCard());
        }
        IPage<Patient> page1 = patientDao.selectPage(page,wrapper);
        //System.out.println("page1 --------------------= " + page1.getTotal());
        return new Result<>(200,"分页查询患者信息成功",page1);
    }

    @Override
    public Result<Patient> getByPatientId(String patientId) {
        QueryWrapper<Patient> wrapper=new QueryWrapper<>();
        if (StringUtils.hasText(patientId)) {
            wrapper.eq("patient_Id",patientId);
        }
        Patient patient = patientDao.selectOne(wrapper);
        return new Result<Patient>(200, "查询患者档案信息成功",patient);
    }


    @Override
    public Result<Patient> getByidCardAll(String idCard) {
        QueryWrapper<Patient> wrapper = new QueryWrapper<>();
        if (StringUtils.hasText(idCard)) {
            wrapper.eq("id_card",idCard);
        }
        Patient ByidCardAll = patientDao.selectOne(wrapper);
        return new Result<>(200,"根据idCard加载信息成功",ByidCardAll);
    }


    @Resource
    private PatientFileDao patientFileDao;
    @Resource
    private CareHistoryDao careHistoryDao;
    @Override
    public Result<Map<String, Object>> patientAllObject(String patientId) {
        System.out.println("patientId========= = " + patientId);
        QueryWrapper<Patient> wrapper = new QueryWrapper<>();
        wrapper.eq("patient_id",patientId);
        Patient patient = patientDao.selectOne(wrapper);

        QueryWrapper<PatientFile> wrapper1 = new QueryWrapper<>();
        wrapper1.eq("patient_id",patientId);
        PatientFile patientFile = patientFileDao.selectOne(wrapper1);

        QueryWrapper<CareHistory> wrapper2 = new QueryWrapper<>();
        wrapper2.eq("patient_id",patientId);
        List<CareHistory> careHistories = careHistoryDao.selectList(wrapper2);

        Map<String, Object> map = new HashMap<>();
        map.put("patientObj",patient);
        map.put("patientFileObj",patientFile);
        map.put("careHistoryObjList",careHistories);

        return new Result<>(200,"患者档案历史病历",map);
    }

    @Resource
    private UserFeign userFeign;
    @Resource
    private DeptFeign deptFeign;
    @Resource
    private RegistrationDao registrationDao;
    @Override
    public Result<String> addPatient(Patient patient1, Registration registration2,Pvo pvo) {
        QueryWrapper<Patient> wrapper2=new QueryWrapper<>();
        wrapper2.eq("id_card",patient1.getIdCard());
        Patient patient = patientDao.selectOne(wrapper2);
        IdWorker idWorker = new IdWorker();//雪花
        int insertPatient=0;
        if (patient == null) {//空则添加
            long patientId = idWorker.nextId();
            String str="HZ"+patientId;//患者id
            patient1.setPatientId(str);
            patient1.setCreateTime(LocalDateTime.now());
            insertPatient = patientDao.insert(patient1);//如果患者库进行添加
        }
        long regId = idWorker.nextId();
        String str1="GH"+ regId;//挂号id
        System.out.println("str1 ================================================================== " + str1);
        registration2.setRegistrationId(str1);
        System.out.println("registration2.getRegistrationId()1111111111111111111 = " + registration2.getRegistrationId());
        registration2.setPatientId(patient1.getPatientId());
        registration2.setPatientName(patient1.getName());
        // 查询出账号登陆者的姓名
        HttpServletRequest request = WebUtil.getRequest();
        String phone = (String) JWTUtils.getInfo(request.getHeader("token")).get("username");
        String userName = userFeign.getByUsername(phone).getUserName();//用户名
        Long userId = userFeign.getByUsername(phone).getUserId();//用户id

        registration2.setUserId(pvo.getUserId());//医生id
        //registration2.setDoctorName();
        String username1 = userFeign.getUsernameById(pvo.getUserId());
        registration2.setDoctorName(username1);

        registration2.setPatientId(patient1.getPatientId());
        LocalDateTime date = LocalDateTime.now();//时间转换
        registration2.setCreateTime(date);
        registration2.setDeptId(pvo.getDeptId());
        registration2.setRegistrationItemId(pvo.getRegItemId());
        registration2.setRegistrationAmount(pvo.getRegItemAmount());
        registration2.setVisitDate(pvo.getVisitDate());//就诊日期
        registration2.setCreateBy(userName);
        //registration2.setSubsectionType("1");//排班时段
        registration2.setSubsectionType(pvo.getSubsectionType());
        registration2.setSchedulingType(pvo.getSchedulingType());//排班类型（急诊门诊）
        registration2.setRegistrationStatus("0");

        Integer regNumber = registrationDao.getRegNumber(pvo.getDeptId());
        if (regNumber == null) {
            regNumber = 0;
        }

        System.out.println("当前科室的挂号数======== = " + regNumber);
        registration2.setRegistrationNumber(regNumber+1);

        System.out.println("===========----------------------------- = " + registration2);
        int insertReg = registrationDao.insert(registration2);

        if(insertReg>0){
            return new Result<>(200,"添加成功",registration2.getRegistrationId());
        }

        return new Result<>(400,"失败");
    }






}
